package com.code.google.p.jyve;

import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.NumberFormat;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

public class GraphWindow {

	private JFrame frmGrafiekopties;
	private JTextField textField_mlmin;
	private JTextField textField_phmin;
	private JTextField textField_mlmax;
	private JTextField textField_phmax;
	private JCheckBox chckbxGridAan;

	private double origMlMin, origMlMax, origPhMin, origPhMax;
	private boolean origGrid;
	private JTextField textField_stapgrootteX;
	private JTextField textField_StapgrootteY;
	private JTextPane error_text;

	private NumberFormat format = NumberFormat.getInstance();

	/**
	 * Create the application.
	 */
	public GraphWindow(Graph graph) {
		try{			
			initialize(graph);		
		}
		catch (Exception e){
			e.printStackTrace();
		}
	}

	private void pasToe(Graph graph){
		error_text.setText(null);
		try {
			graph.setRange(format.parse(textField_mlmin.getText()).doubleValue(), 
					format.parse(textField_mlmax.getText()).doubleValue(), 
					format.parse(textField_phmin.getText()).doubleValue(), 
					format.parse(textField_phmax.getText()).doubleValue());
			graph.setGrid(chckbxGridAan.isSelected());
			graph.setStep(format.parse(textField_stapgrootteX.getText()).doubleValue(), 
					format.parse(textField_StapgrootteY.getText()).doubleValue());
		}
		catch (ParseException e){
			error_text.setText("Niet alle getallen zijn geldig.");
		}
	}

	private void herstelOud(Graph graph) {
		graph.setRange(origMlMin, origMlMax, origPhMin, origPhMax);
		graph.setGrid(origGrid);
	}

	private void setOriginal(Graph graph){
		origMlMin = graph.getMinML();
		origMlMax = graph.getMaxML();
		origPhMin = graph.getMinPH();
		origPhMax = graph.getMaxPH();
		origGrid = graph.getGrid();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize(final Graph graph) {
		setOriginal(graph);
		frmGrafiekopties = new JFrame();
		frmGrafiekopties.setIconImage(Toolkit.getDefaultToolkit().getImage(GraphWindow.class.getResource("/com/code/google/p/jyve/Jyve3.png")));
		frmGrafiekopties.getContentPane().setBackground(Color.WHITE);
		frmGrafiekopties.setTitle("Grafiekopties");
		frmGrafiekopties.setBounds(100, 100, 450, 300);
		frmGrafiekopties.getContentPane().setLayout(null);

		JButton btnToepassen = new JButton("Toepassen");
		btnToepassen.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				pasToe(graph);
			}
		});
		btnToepassen.setBounds(10, 228, 128, 23);
		frmGrafiekopties.getContentPane().add(btnToepassen);

		JButton btnAnnuleren = new JButton("Annuleren");
		btnAnnuleren.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				herstelOud(graph);
				hide();
			}
		});
		btnAnnuleren.setBounds(296, 228, 128, 23);
		frmGrafiekopties.getContentPane().add(btnAnnuleren);

		JLabel lblAsopties = new JLabel("Asopties");
		lblAsopties.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblAsopties.setBounds(10, 11, 110, 14);
		frmGrafiekopties.getContentPane().add(lblAsopties);

		JLabel lblHorizontaleAs = new JLabel("Horizontale as (mL)");
		lblHorizontaleAs.setBounds(10, 36, 110, 14);
		frmGrafiekopties.getContentPane().add(lblHorizontaleAs);

		JLabel lblVerticaleAsph = new JLabel("Verticale as (pH)");
		lblVerticaleAsph.setBounds(209, 36, 110, 14);
		frmGrafiekopties.getContentPane().add(lblVerticaleAsph);

		JLabel lblMinimum = new JLabel("Minimum");
		lblMinimum.setBounds(20, 61, 79, 14);
		frmGrafiekopties.getContentPane().add(lblMinimum);

		textField_mlmin = new JTextField();
		textField_mlmin.setBounds(10, 75, 86, 20);
		frmGrafiekopties.getContentPane().add(textField_mlmin);
		textField_mlmin.setColumns(10);
		textField_mlmin.setText(format.format(graph.getMinML()));

		JLabel lblMinimum_1 = new JLabel("Minimum");
		lblMinimum_1.setBounds(219, 61, 76, 14);
		frmGrafiekopties.getContentPane().add(lblMinimum_1);

		textField_phmin = new JTextField();
		textField_phmin.setBounds(209, 75, 86, 20);
		frmGrafiekopties.getContentPane().add(textField_phmin);
		textField_phmin.setColumns(10);
		textField_phmin.setText(format.format(graph.getMinPH()));

		JLabel lblMaximum = new JLabel("Maximum");
		lblMaximum.setBounds(20, 106, 79, 14);
		frmGrafiekopties.getContentPane().add(lblMaximum);

		JLabel lblMaximum_1 = new JLabel("Maximum");
		lblMaximum_1.setBounds(219, 106, 76, 14);
		frmGrafiekopties.getContentPane().add(lblMaximum_1);

		textField_mlmax = new JTextField();
		textField_mlmax.setBounds(10, 120, 86, 20);
		frmGrafiekopties.getContentPane().add(textField_mlmax);
		textField_mlmax.setColumns(10);
		textField_mlmax.setText(format.format(graph.getMaxML()));

		textField_phmax = new JTextField();
		textField_phmax.setBounds(209, 120, 86, 20);
		frmGrafiekopties.getContentPane().add(textField_phmax);
		textField_phmax.setColumns(10);
		textField_phmax.setText(format.format(graph.getMaxPH()));

		chckbxGridAan = new JCheckBox("Grid aan");
		chckbxGridAan.setSelected(true);
		chckbxGridAan.setBounds(10, 198, 97, 23);
		frmGrafiekopties.getContentPane().add(chckbxGridAan);

		JButton btnGereed = new JButton("Gereed");
		btnGereed.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				hide();
			}
		});
		btnGereed.setBounds(148, 228, 138, 23);
		frmGrafiekopties.getContentPane().add(btnGereed);

		JLabel lblStapgrootte = new JLabel("Stapgrootte");
		lblStapgrootte.setBounds(20, 151, 79, 14);
		frmGrafiekopties.getContentPane().add(lblStapgrootte);

		textField_stapgrootteX = new JTextField();
		textField_stapgrootteX.setBounds(10, 171, 86, 20);
		frmGrafiekopties.getContentPane().add(textField_stapgrootteX);
		textField_stapgrootteX.setColumns(10);
		textField_stapgrootteX.setText(format.format(graph.getStepX()));

		JLabel lblNewLabel = new JLabel("Stapgrootte");
		lblNewLabel.setBounds(219, 151, 76, 14);
		frmGrafiekopties.getContentPane().add(lblNewLabel);

		textField_StapgrootteY = new JTextField();
		textField_StapgrootteY.setBounds(209, 171, 86, 20);
		frmGrafiekopties.getContentPane().add(textField_StapgrootteY);
		textField_StapgrootteY.setColumns(10);
		textField_StapgrootteY.setText(format.format(graph.getStepY()));

		error_text = new JTextPane();
		error_text.setBounds(314, 30, 110, 161);
		frmGrafiekopties.getContentPane().add(error_text);
	}

	public void show(){
		frmGrafiekopties.setVisible(true);
	}

	public void hide(){
		frmGrafiekopties.setVisible(false);
	}
}
